//
//  HBChapterModel.m
//  HeartBeat
//
//  Created by karong on 2023/8/23.
//

#import "HBChapterModel.h"
#import "HBReadSettingManager.h"
@implementation HBChaptersModel
+ (NSDictionary *)mj_objectClassInArray {
    return @{@"items":NSClassFromString(@"HBChapterModel")};
}
@end

@implementation HBChapterModel
- (NSInteger)pageCount{
    return self.pageArray.count;
}

- (NSArray *)positionDatas{
    return self.pageArray;
}

- (NSInteger)getChangeIndex:(NSNumber *)position{
    if (position.integerValue <= 0) {
        return 0;
    }
    __block NSInteger index = 0;
    [self.pageArray enumerateObjectsUsingBlock:^(NSNumber*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.integerValue >= position.integerValue - 30) {
            index = idx;
            *stop = YES;
        }
    }];
    return index;
}

///分页，pageArray记录分页节点
- (void)setContentPage {
    [self setPageBound:CGRectMake(16, 20, HBScreenWidth - 32, HBScreenHeight - (20 + HBStatusBarHeight + 20 + HBSafeAreaBottomHeight))];
    
}

- (void)setPageBound:(CGRect)bounds {
    self.pageArray = @[].mutableCopy;//666D73
    NSMutableAttributedString *attr = [[NSMutableAttributedString  alloc] initWithString:[HBTool newContent:self.content] attributes:[HBReadSettingManager defultAttFontDic]];
    NSMutableAttributedString *attrTitle = [[NSMutableAttributedString  alloc] initWithString:SF(@"\n\n%@\n\n",self.title) attributes:@{NSFontAttributeName: [UIFont fontWithName:[[HBReadSettingManager shared] showModel].fontName size:[[HBReadSettingManager shared] showModel].font],NSForegroundColorAttributeName:[UIColor colorFromHexRGB:[[HBReadSettingManager shared] showModel].colorStr]}];

    ///这里加上title
    [attr insertAttributedString:attrTitle atIndex:0];
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef) attr);
    CGPathRef path = CGPathCreateWithRect(bounds, NULL);
    CFRange range = CFRangeMake(0, 0);
    NSUInteger rangeOffset = 0;
    do {
        CTFrameRef frame;
        frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(rangeOffset, 0), path, NULL);
        range = CTFrameGetVisibleStringRange(frame);
        rangeOffset += range.length;
        [self.pageArray addObject:@(range.location)];
        if (frame) {
            CFRelease(frame);
        }
    } while (range.location + range.length < attr.length);
    if (path) {
        CFRelease(path);
    }
    if (frameSetter) {
        CFRelease(frameSetter);
    }
    self.attributedString = attr;
}

///获取某一页内容
- (NSAttributedString *)getContentAtt:(NSInteger)page {
    page = (page >= self.pageArray.count) ? self.pageArray.count - 1 : page;
    
    if (page < self.pageArray.count) {
        NSUInteger loc = [self.pageArray[page] integerValue];
        NSUInteger len = 0;
        if (page == self.pageArray.count - 1) {
            len = self.attributedString.length - loc;
        } else {
            len = [self.pageArray[page + 1] integerValue] - loc;
        }
        NSAttributedString *att = [_attributedString attributedSubstringFromRange:NSMakeRange(loc, len)];
        return att;
    }
    return [[NSAttributedString alloc] initWithString:WSLocalized(@"更多精彩內容盡在發現\n\r\n\r數據加載中...\n\r\n\r請耐心等待") attributes:[HBReadSettingManager defultAttFontDic]];
}
@end
